package com.controller;

import com.bean.Commodity;
import com.bean.Provider;
import com.bean.Type;
import com.service.CommodityService;
import com.service.TypeService;
import com.util.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 界面业务控制器
 */
@Controller
@RequestMapping("commodity")
public class CommodityController {

    @Autowired
    private CommodityService commodityService;
    @Autowired
    private TypeService typeService;

    private ParamUtil paramUtil = new ParamUtil();

    @RequestMapping("view")
    public String view(Model model, Commodity commodity){
        System.out.println(">>>>   provider view");
        System.out.println("-----------------------------------------");

        List<Commodity> commodityList = commodityService.find(commodity);
        model.addAttribute("commodityView",commodityList.get(0));

        return "/provider/detail";

    }

    @RequestMapping("updatePage")
    public String updatePage(Model model, Commodity commodity, HttpServletRequest request, HttpSession session){
        System.out.println(">>>>   commodity updatePage");
        System.out.println("-----------------------------------------");

        System.out.println(commodity);

//        session.setAttribute("prevUrl", session.getAttribute("currUrl"));
//        session.setAttribute("prevParam", session.getAttribute("currParam"));
//
//        session.setAttribute("currUrl", "/user/updatePage");
//        session.setAttribute("currParam", paramUtil.getParamStr(request));

        List<Commodity> commodityList = commodityService.find(commodity);
        model.addAttribute("commodityInfo",commodityList.get(0));
        model.addAttribute("typeList",typeService.find(new Type()));

        System.out.println(commodityList.get(0));

        return "/commodity/commodityUpdate";
    }

    @RequestMapping("update")
    @ResponseBody
    public Map<String,Object> update(Commodity commodity, HttpSession session) {
        System.out.println(">>>>   commodity update");
        System.out.println("-----------------------------------------");
        Map<String, Object> map = new HashMap<>();

        try {
            commodityService.modify(commodity);
            map.put("msg","修改成功");
            map.put("backUrl","/main");

            session.setAttribute("currUrl","/provider/updatePage");
            session.setAttribute("currParam","id="+commodity.getProviderId());

        } catch (Exception e) {
            System.err.println(e);
            map.put("msg","修改失败");
        }
        return map;
    }

    @RequestMapping("delete")
    @ResponseBody
    public Map<String,Object> delete(Commodity commodity){
        System.out.println(">>>>   commodity delete");
        System.out.println("-----------------------------------------");

        Map<String,Object> map = new HashMap<>();

        try {
            commodityService.remove(commodity);
            map.put("msg","删除成功");
        } catch (Exception e) {
            map.put("msg","删除失败");
        }

        map.put("backUrl","/provider/updatePage");
        map.put("backParam","id=" + commodity.getProviderId());
        return map;
    }



    @RequestMapping("addPage")
    public String addPage(Model model, Provider provider){
        System.out.println(">>>>   commodity addPage");
        System.out.println("-----------------------------------------");

        List<Type> typeList = typeService.find(new Type());
        model.addAttribute("typeList",typeList);
        model.addAttribute("providerId",provider.getId());

        return "/commodity/commodityAdd";
    }


    @RequestMapping("add")
    @ResponseBody
    public Map<String,Object> add(Commodity commodity, HttpSession session){
        System.out.println(">>>>   commodity add");
        System.out.println("-----------------------------------------");

        System.out.println(commodity);

        Map<String,Object> map = new HashMap<>();
        if (commodity.getName() != null && !commodity.getName().trim().equals("")){
            map.put("name",commodity.getName());
            System.out.println(commodity.getName());
        }
        map.put("providerId",commodity.getProviderId());

        List<Commodity> query = commodityService.query(map);

        map.clear();

        map.put("backUrl","/main");
        session.setAttribute("currUrl","/provider/updatePage");
        session.setAttribute("currParam","id="+commodity.getProviderId());

        if (query.size() > 0){
            map.put("msg","该供货商已存在该商品");
            return map;
        }

        try {
            commodityService.save(commodity);
            map.put("msg","添加成功");

        } catch (Exception e) {
            map.put("msg","添加失败");
        }

        return map;
    }
}













